package com.jic.market.mapper.skill;

import com.jic.market.entity.skill.ActivityMerchantConfig;

import com.jic.market.request.CouponDetailRequest;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 商家配置表
 *
 * @author : seven
 * @version : v1.0.0
 * @email : 774329481@qq.com
 * @since : 2020-03-12 04:52:22
 */
@Repository
public interface ActivityMerchantConfigMapper {

    long insert(ActivityMerchantConfig activityMerchantConfig);

    long delete(@Param("promotionId") String promotionId);

    long update(ActivityMerchantConfig activityMerchantConfig);

    ActivityMerchantConfig queryByPrimaryKey(Long id);

    List<ActivityMerchantConfig> query(ActivityMerchantConfig activityMerchantConfig);

    List<ActivityMerchantConfig> queryPage(@Param("startIndex") int startIndex, @Param("limit") int limit, @Param("activityMerchantConfig") ActivityMerchantConfig activityMerchantConfig);

    long queryPageCount(ActivityMerchantConfig activityMerchantConfig);

//    @Select("select * from market_activity_merchant_config where promotion_id=#{promotionId} and delete_flag='0'" +
//            "<if test='start != null and limit != null'>" +
//            "limit #{start} , #{limit}"  +
//            "</if>")

    /**
     *
     * @param promotionId
     * @param start
     * @param limit
     * @return
     */
    @Select({
            "<script>",
            " select * from market_activity_merchant_config where promotion_id=#{promotionId} and delete_flag='0'",
            "<if test=\"merchantIds !=null and merchantIds.size >0\"> ",
            "    and merchant_id in ",
                "<foreach collection=\"merchantIds\" item=\"merchantId\" index=\"index\"",
    "                         open=\"(\" close=\")\" separator=\",\">",
    "                    #{merchantId}",
    "            </foreach>",
            "</if> ",
                "<if test=\"couponVo !=null and couponVo.orgNumber != null and couponVo.orgNumber != ''\"> ",
                    "and org_number =  #{couponVo.orgNumber}  ",
                "</if>",
                "<if test=\"couponVo !=null and couponVo.merchantName != null and couponVo.merchantName != ''\"> ",
                    "and merchant_name like  '%' #{couponVo.merchantName} '%'  ",
                "</if>",
                "<trim>",
                    "<if test=\"start != null and limit != null \"> ",
                    "  limit #{start} , #{limit}",
                    "</if> ",
                "</trim>",
            "</script>"
    })
    List<ActivityMerchantConfig> queryByPromotionId(@Param("promotionId")String promotionId , @Param("start") Integer start , @Param("limit") Integer limit,@Param("merchantIds") List<Long> merchantIds,@Param("couponVo") CouponDetailRequest couponVo);

    @Update("update market_activity_merchant_config set delete_flag='1' where promotion_id=#{promotionId}" +
            " and merchant_id=#{merchantId}")
    long deleteByPromotionIdAndMerchantId(@Param("promotionId")String promotionId,
                                          @Param("merchantId")String merchantId);
    @Select({
            "<script>",
            "select count(1) from market_activity_merchant_config where promotion_id=#{promotionId} and delete_flag='0'",
            "<if test=\"merchantIds !=null and merchantIds.size >0\">",
            " and merchant_id in ",
            "<foreach collection=\"merchantIds\" item=\"merchantId\" index=\"index\"",
                    "                         open=\"(\" close=\")\" separator=\",\">",
                    "                    #{merchantId}",
                    "                </foreach>",
            "</if> ",
            "<if test=\"couponVo !=null and couponVo.orgNumber != null and couponVo.orgNumber != ''\"> ",
            " and org_number =  #{couponVo.orgNumber}  ",
            "</if>",
            "<if test=\"couponVo !=null and couponVo.merchantName != null and couponVo.merchantName != ''\"> ",
            " and merchant_name like  '%' #{couponVo.merchantName} '%'  ",
            "</if>",
            "</script>"
    })
    Long queryByPromotionIdTotal(@Param("promotionId") String promotionId,@Param("merchantIds") List<Long> merchantIds ,@Param("couponVo") CouponDetailRequest promotionIdRequest);

    @Select("select merchant_id from market_activity_merchant_config where promotion_id = #{promotionId} and delete_flag = '0'")
    List<Long> queryMerchantIdsByPromotionId(@Param("promotionId") String promotionId);

    int insertBatch(@Param("items") List<ActivityMerchantConfig> merchantConfigList);
}